package com.gxa.clientkill.controller;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.gxa.clientkill.pojo.Luck;
import com.gxa.clientkill.service.LuckService;
import com.gxa.clientkill.utils.JsonResult;
import io.swagger.annotations.*;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Jun
 * @version 1.0
 * @date 2020/11/11 10:17
 */

@Api(value = "实现抽奖功能的控制器",tags = "抽奖接口的api")
@RestController
@RequestMapping("/luck")
public class LuckController {


    @Resource
    LuckService luckService;

    JsonResult jsonResult = new JsonResult();

    @ApiOperation(value = "修改接口", notes = "抽奖活动的修改", httpMethod = "POST", response = Map.class)
    @ApiResponses({
            @ApiResponse(code = 200, message = "请求成功"),
            @ApiResponse(code = 404, message = "找不到页面"),
            @ApiResponse(code = 415, message = "数据类型格式不正确"),
            @ApiResponse(code = 500, message = "后台服务错误")
    })
    @PostMapping("/update")
    public Map update(@RequestBody @ApiParam(name = "luck",value = "抽奖活动的pojo信息",required = true) Luck luck) {
        Integer update = luckService.update(luck);
        jsonResult.setData(luck);
        jsonResult.setCode("200");
        jsonResult.setMsg("修改成功");
        return jsonResult.getValues();
    }

    @PostMapping("/list")
    @ApiOperation(value = "分页查询接口", notes = "用于抽奖活动的列表展示", httpMethod = "POST", response = Map.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "limit", value = "分页大小", required = true, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "page", value = "当前页", required = true, dataType = "int", paramType = "query")
    })
    @ApiResponses({
            @ApiResponse(code = 200, message = "请求成功"),
            @ApiResponse(code = 404, message = "找不到页面"),
            @ApiResponse(code = 415, message = "数据类型格式不正确"),
            @ApiResponse(code = 500, message = "后台服务错误")
    })
    public Map list(@RequestParam("limit") int limit, @RequestParam("page") int page) {
        //定义一个map
        Map<String, Object> luckMap = new HashMap<>();
        //将分页条件输入PageHelper
        Page<Object> page1 = PageHelper.startPage(page, limit);
        //将所有的优惠券信息查询出来
        List<Luck> lucks = luckService.list();
        //将分页信息与所有的优惠券信息结合
        PageInfo<Luck> info = new PageInfo<>(lucks);
        //将分页的列表导入map中
        luckMap.put("list",info.getList());
        //将总记录数导入map中
        luckMap.put("counts",info.getTotal());
        jsonResult.put("counts",luckMap.get("counts"));
        jsonResult.setData(luckMap.get("list"));
        jsonResult.setCode("200");
        jsonResult.setMsg("分页查询成功");
        return jsonResult.getValues();
    }

    @PostMapping("/add")
    @ApiOperation(value = "抽奖添加接口", notes = "该接口用于添加抽奖活动", httpMethod = "POST", response = Map.class)
    @ApiResponses({
            @ApiResponse(code = 200, message = "请求成功"),
            @ApiResponse(code = 404, message = "找不到页面"),
            @ApiResponse(code = 415, message = "数据类型格式不正确"),
            @ApiResponse(code = 500, message = "后台服务错误")
    })
    public Map add(@RequestBody @ApiParam(name = "luck",value = "抽奖活动的pojo信息",required = true) Luck luck) {
        luckService.add(luck);
        jsonResult.setData(luck);
        jsonResult.setCode("200");
        jsonResult.setMsg("添加成功");
        return jsonResult.getValues();
    }

    @PostMapping("/sel")
    @ApiOperation(value = "查询抽奖", notes = "该接口用于查询抽奖的详细信息", httpMethod = "POST", response = Map.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "抽奖id", required = true, dataType = "int", paramType = "query"),
    })
    @ApiResponses({
            @ApiResponse(code = 200, message = "请求成功"),
            @ApiResponse(code = 404, message = "找不到页面"),
            @ApiResponse(code = 415, message = "数据类型格式不正确"),
            @ApiResponse(code = 500, message = "后台服务错误")
    })
    public Map sel(@RequestParam("id") int id) {
        Luck luck = luckService.sel(id);
        jsonResult.setData(luck);
        jsonResult.setCode("200");
        jsonResult.setMsg("查询成功");
        return jsonResult.getValues();
    }

    @PostMapping("/del")
    @ApiOperation(value = "抽奖删除", notes = "该接口用于删除抽奖", httpMethod = "POST", response = Map.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "抽奖id", required = true, dataType = "int", paramType = "query"),
    })
    @ApiResponses({
            @ApiResponse(code = 200, message = "请求成功"),
            @ApiResponse(code = 404, message = "找不到页面"),
            @ApiResponse(code = 415, message = "数据类型格式不正确"),
            @ApiResponse(code = 500, message = "后台服务错误")
    })
    public Map del(@RequestParam("id") int id) {
        Integer del = luckService.del(id);
        jsonResult.setData(del);
        jsonResult.setCode("200");
        jsonResult.setMsg("删除成功");
        return jsonResult.getValues();
    }

    @PostMapping("/rec")
    @ApiOperation(value = "首页推荐", notes = "该接口用于列表中设置首页推荐", httpMethod = "POST", response = Map.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "recommend", value = "抽奖推荐状态", required = true, dataType = "int", paramType = "query"),
    })
    @ApiResponses({
            @ApiResponse(code = 200, message = "请求成功"),
            @ApiResponse(code = 404, message = "找不到页面"),
            @ApiResponse(code = 415, message = "数据类型格式不正确"),
            @ApiResponse(code = 500, message = "后台服务错误")
    })
    public Map rec(@RequestBody @ApiParam(name = "luck",value = "抽奖活动的pojo信息",required = true) Luck luck) {
        Integer rec = luckService.rec(luck);
        jsonResult.setData(rec);
        jsonResult.setCode("200");
        jsonResult.setMsg("删除成功");
        return jsonResult.getValues();
    }

    @PostMapping("/detail")
    @ApiOperation(value = "抽奖详情列表接口", notes = "用于抽奖详情的列表展示", httpMethod = "POST", response = Map.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "limit", value = "分页大小", required = true, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "page", value = "当前页", required = true, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "num", value = "团购编号", required = false, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "state", value = "拼团状态", required = false, dataType = "int", paramType = "query")
    })
    @ApiResponses({
            @ApiResponse(code = 200, message = "请求成功"),
            @ApiResponse(code = 404, message = "找不到页面"),
            @ApiResponse(code = 415, message = "数据类型格式不正确"),
            @ApiResponse(code = 500, message = "后台服务错误")
    })
    public Map detail(@RequestParam("limit") int limit, @RequestParam("page") int page, @RequestParam("num") String num, @RequestParam("state") int state) {


        return jsonResult.getValues();
    }

    @PostMapping("/get")
    @ApiOperation(value = "中奖用户列表接口", notes = "用于中奖用户详情的列表展示", httpMethod = "POST", response = Map.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "limit", value = "分页大小", required = true, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "page", value = "当前页", required = true, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "state", value = "中奖状态", required = true, dataType = "int", paramType = "query")
    })
    @ApiResponses({
            @ApiResponse(code = 200, message = "请求成功"),
            @ApiResponse(code = 404, message = "找不到页面"),
            @ApiResponse(code = 415, message = "数据类型格式不正确"),
            @ApiResponse(code = 500, message = "后台服务错误")
    })
    public Map get(@RequestParam("limit") int limit, @RequestParam("page") int page, @RequestParam("state") int state) {

        //定义一个map
        Map<String, Object> luckMap = new HashMap<>();
        //将分页条件输入PageHelper
        Page<Object> page1 = PageHelper.startPage(page, limit);
        //将所有的优惠券信息查询出来
        List<Luck> lucks = luckService.list();
        //将分页信息与所有的优惠券信息结合
        PageInfo<Luck> info = new PageInfo<>(lucks);
        //将分页的列表导入map中
        luckMap.put("list",info.getList());
        //将总记录数导入map中
        luckMap.put("counts",info.getTotal());
        jsonResult.put("counts",luckMap.get("counts"));
        jsonResult.setData(luckMap.get("list"));
        jsonResult.setCode("200");
        jsonResult.setMsg("分页查询成功");
        return jsonResult.getValues();

    }

    @PostMapping("/inList")
    @ApiOperation(value = "内定中奖用户列表接口", notes = "用于内定中奖用户详情的列表展示", httpMethod = "POST", response = Map.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "limit", value = "分页大小", required = true, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "page", value = "当前页", required = true, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "state", value = "中奖状态", required = true, dataType = "int", paramType = "query")
    })
    @ApiResponses({
            @ApiResponse(code = 200, message = "请求成功"),
            @ApiResponse(code = 404, message = "找不到页面"),
            @ApiResponse(code = 415, message = "数据类型格式不正确"),
            @ApiResponse(code = 500, message = "后台服务错误")
    })
    public Map inList(@RequestParam("limit") int limit, @RequestParam("page") int page, @RequestParam("state") int state) {

        return jsonResult.getValues();
    }

    @ApiOperation(value = "内定中奖接口", notes = "用于内定中奖的用户", httpMethod = "POST", response = Map.class)
    @ApiResponses({
            @ApiResponse(code = 200, message = "请求成功"),
            @ApiResponse(code = 404, message = "找不到页面"),
            @ApiResponse(code = 415, message = "数据类型格式不正确"),
            @ApiResponse(code = 500, message = "后台服务错误")
    })
    @PostMapping("/inside")
    public Map inside(@RequestBody @ApiParam(name = "luck",value = "抽奖活动的pojo信息",required = true) Luck luck) {
        jsonResult.setData(luck);
        return jsonResult.getValues();
    }

    @ApiOperation(value = "随机中奖接口", notes = "用于随机抽取中奖的用户", httpMethod = "POST", response = Map.class)
    @ApiResponses({
            @ApiResponse(code = 200, message = "请求成功"),
            @ApiResponse(code = 404, message = "找不到页面"),
            @ApiResponse(code = 415, message = "数据类型格式不正确"),
            @ApiResponse(code = 500, message = "后台服务错误")
    })
    @PostMapping("/random")
    public Map random(@RequestBody @ApiParam(name = "luck",value = "抽奖活动的pojo信息",required = true) Luck luck) {
        jsonResult.setData(luck);
        return jsonResult.getValues();
    }

    @PostMapping("/dsel")
    @ApiOperation(value = "前台幸运抽奖", notes = "该接口用四种抽奖详情的查询", httpMethod = "POST", response = Map.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "抽奖id", required = true, dataType = "int", paramType = "query"),
    })
    @ApiResponses({
            @ApiResponse(code = 200, message = "请求成功"),
            @ApiResponse(code = 404, message = "找不到页面"),
            @ApiResponse(code = 415, message = "数据类型格式不正确"),
            @ApiResponse(code = 500, message = "后台服务错误")
    })
    public Map dsel(@RequestParam("id") int id) {
        Luck luck = new Luck();
        luck.setId(id);
        jsonResult.setData(luck);
        return jsonResult.getValues();
    }

    @PostMapping("/getUser")
    @ApiOperation(value = "商品中奖用户接口", notes = "用于查询该商品中奖的用户展示", httpMethod = "POST", response = Map.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "state", value = "中奖状态", required = true, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "num", value = "商品编号", required = true, dataType = "string", paramType = "query"),
    })
    @ApiResponses({
            @ApiResponse(code = 200, message = "请求成功"),
            @ApiResponse(code = 404, message = "找不到页面"),
            @ApiResponse(code = 415, message = "数据类型格式不正确"),
            @ApiResponse(code = 500, message = "后台服务错误")
    })
    public Map getUser(@RequestParam("state") int state,@RequestParam("num") int num) {
        Luck luck = new Luck();
        luck.setState(state);
        jsonResult.setData(luck);
        return jsonResult.getValues();
    }
}
